import request from '../utils/request';

/**
 * 创建升级包
 * @param {string} name 升级包名字
 * @param {string} description 升级包描述
 * @param {string} versionName 版本名称
 * @param {string} packageUrl 升级包地址
 * @param {number} size 升级包大小
 * @param {boolean} active 允许升级
 * @param {string} productTypeUuid 设备类型uuid
 */
export function createPackageApi(name, description, versionName, packageUrl, size, active, productTypeUuid) {
  return request({
    url: '/packages',
    method: 'post',
    data: {
      name: name,
      description: description || null,
      version_name: versionName,
      package_url: packageUrl,
      size: size,
      active: active,
      product_type_uuid: productTypeUuid || null
    }
  });
}

/**
 * 删除升级包
 * @param {string} id
 */
export function deletePackageApi(id) {
  return request({
    url: '/packages/' + id,
    method: 'delete'
  });
}

/**
 *  更新升级包
 * @param {string} id
 * @param {string} name
 * @param {string} description
 * @param {string} versionName 版本名称
 * @param {string} packageUrl 升级包地址
 * @param {number} size 升级包大小
 * @param {boolean} active 允许升级
 * @param {string} productTypeUuid 设备类型uuid
 */
export function updatePackageApi(id, name, description, versionName, packageUrl, size, active, productTypeUuid) {
  return request({
    url: '/packages/' + id,
    method: 'put',
    data: {
      name: name,
      description: description || null,
      version_name: versionName,
      package_url: packageUrl,
      size: size,
      active: active,
      product_type_uuid: productTypeUuid || null
    }
  });
}

/**
 * 获取升级包列表
 * @param {number} pageIndex 页码下标，默认从0开始
 * @param {number} pageSize 每页大小
 * @param {string} sortType 正序or倒序
 * @param {string} productTypeUuid 设备类型uuid
 * @param {boolean} active 允许升级
 * @param {string} name 固件包
 * @param {string} description 备注
 */
export function getPackageListApi(pageIndex, pageSize, sortType, productTypeUuid, name, description, active) {
  return request({
    url: '/packages',
    method: 'get',
    params: Object.assign(
      {
        page_index: pageIndex,
        page_size: pageSize,
        sort_type: sortType 
      },
      productTypeUuid ? { product_type_uuid: productTypeUuid } : null,
      typeof active === 'boolean' ? { active: active } : null,
      name ? { name: name } : null,
      description ? { description: description } : null
    )
  });
}

/**
 * 查询升级包接口
 * @param {string} name 升级包名称
 * @param {number} take 获取数量 5-100
 * @param {string} productTypeUuid 产品类型uuid
 */
export function queryPackageListApi(name, take, productTypeUuid) {
  return request({
    url: '/packages/query',
    method: 'get',
    params: Object.assign(
      {
        take: take
      },
      name ? { name } : null,
      productTypeUuid ? { product_type_uuid: productTypeUuid } : null
    )
  });
}
